package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.trade.NativePayApi;
import com.jzo2o.api.trade.dto.request.NativePayReqDTO;
import com.jzo2o.api.trade.dto.response.NativePayResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author Zhy
 * @version 1.0
 * @date 2025-04-01 16:35
 */
@Component
@Slf4j
public class TradeClient {

	@Resource
	private NativePayApi nativePayApi;

	/**
	 * 生成二维码
	 *
	 * @return:
	 * @param:
	 */
	@SentinelResource(value = "findNativePay", fallback = "findNativePayFallback", blockHandler = "findNativePayBlockHandler")
	public NativePayResDTO findNativePay(
		NativePayReqDTO nativePayDTO
	) {
		return nativePayApi.createDownLineTrading(nativePayDTO);
	}

	/**
	 * 降级方法
	 *
	 * @return:
	 * @param:
	 */
	public NativePayResDTO findNativePayFallback(NativePayReqDTO nativePayDTO, Throwable t) {
		log.error("生成支付二维码失败，服务降级，返回默认值，错误信息：{}", t.getMessage());
		NativePayResDTO dto = new NativePayResDTO();
		dto.setProductOrderNo(-1L);
		return dto;
	}

	/**
	 * 熔断方法
	 *
	 * @return:
	 * @param:
	 */
	public NativePayResDTO findNativePayBlockHandler(NativePayReqDTO nativePayDTO, BlockException be) {
		log.error("生成支付二维码失败，熔断，返回默认值，错误信息：{}", be.getMessage());
		NativePayResDTO dto = new NativePayResDTO();
		dto.setProductOrderNo(-2L);
		return dto;
	}

}
